package com.tjec.project.system.controller;

import com.tjec.common.enums.BusinessCategoryUser;
import com.tjec.common.enums.ErrCode;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.security.service.TokenService;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.framework.web.domain.TreeSelectAndChildrenNum;
import com.tjec.project.system.domain.BmsMdBusinessCategory;
import com.tjec.project.system.service.IBmsMdBusinessCategoryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务类别管理Controller
 *
 * @author tjec
 * @date 2020-04-03
 */
@Api(value="业务类别管理", tags = {"业务类别管理接口"})
@RestController
@RequestMapping("/system/businessCategory")
public class BmsMdBusinessCategoryController extends BaseController {
    @Autowired
    private IBmsMdBusinessCategoryService bmsMdBusinessCategoryService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询业务类别管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:businessCategory:list')")
    @GetMapping("/list")
    public AjaxResult list(BmsMdBusinessCategory bmsMdBusinessCategory) {
        List<BmsMdBusinessCategory> list = bmsMdBusinessCategoryService.selectBmsMdBusinessCategoryList(bmsMdBusinessCategory);
        return AjaxResult.success(list);
    }

    /**
     * 导出业务类别管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:businessCategory:export')")
    @Log(title = "业务类别管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmsMdBusinessCategory bmsMdBusinessCategory) {
        List<BmsMdBusinessCategory> list = bmsMdBusinessCategoryService.selectBmsMdBusinessCategoryList(bmsMdBusinessCategory);
        ExcelUtil<BmsMdBusinessCategory> util = new ExcelUtil<BmsMdBusinessCategory>(BmsMdBusinessCategory.class);
        return util.exportExcel(list, "businessCategory");
    }

    /**
     * 获取业务类别管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:businessCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(bmsMdBusinessCategoryService.selectBmsMdBusinessCategoryById(id));
    }
    
    /**
     * 获取业务类别管理详细信息
     */
    @GetMapping(value = "/getInfoByGuid/{guid}")
    public AjaxResult getInfoByGuid(@PathVariable("guid") String guid) {
        return AjaxResult.success(bmsMdBusinessCategoryService.selectBmsMdBusinessCategoryByGuIdLimit(guid));
    }

    /**
     * 新增业务类别管理
     */
    @PreAuthorize("@ss.hasPermi('system:businessCategory:add')")
    @Log(title = "业务类别管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsMdBusinessCategory bmsMdBusinessCategory) {
        return toAjax(bmsMdBusinessCategoryService.insertBmsMdBusinessCategory(bmsMdBusinessCategory));
    }

    /**
     * 修改业务类别管理
     */
    @PreAuthorize("@ss.hasPermi('system:businessCategory:edit')")
    @Log(title = "业务类别管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsMdBusinessCategory bmsMdBusinessCategory) {
    	bmsMdBusinessCategory.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
    	int res = bmsMdBusinessCategoryService.updateBmsMdBusinessCategory(bmsMdBusinessCategory);
        return toAjax(res);
    }

    /**
     * 删除业务类别管理
     */
    @PreAuthorize("@ss.hasPermi('system:businessCategory:remove')")
    @Log(title = "业务类别管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(bmsMdBusinessCategoryService.deleteBmsMdBusinessCategoryByIds(ids));
    }

    /**
     * 获取业务类别下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(BmsMdBusinessCategory bmsMdBusinessCategory) {
        List<BmsMdBusinessCategory> businessCategories = bmsMdBusinessCategoryService.selectBmsMdBusinessCategoryList(bmsMdBusinessCategory);
        return AjaxResult.success(bmsMdBusinessCategoryService.buildBusinessTreeSelect(businessCategories));
    }

    /**
     * 获取全部业务类别下拉树列表
     */
    @GetMapping("/allTreeselectAndChildrenNum")
    public AjaxResult allTreeselectAndChildrenNum(BmsMdBusinessCategory businessCategory, BusinessCategoryUser tableName) {
        if (null == tableName) {
            return  new AjaxResult(ErrCode.SYS_PARAMETER_EMPTY.getErrCode(), ErrCode.SYS_PARAMETER_EMPTY.getErrMsg());
        }
        List<BmsMdBusinessCategory> businessCategorys = bmsMdBusinessCategoryService.selectBusinessCategoryAndChildrenNumList(businessCategory, tableName.name());
        List<TreeSelectAndChildrenNum> list = bmsMdBusinessCategoryService.buildBusinessTreeSelectAndChildrenNum(businessCategorys ,false);
        return AjaxResult.success(list);
    }

    /**
     * 获取业务类别下拉树列表
     */
    @GetMapping("/treeselectAndChildrenNum")
    public AjaxResult treeselectAndChildrenNum(BmsMdBusinessCategory businessCategory, BusinessCategoryUser tableName) {
        if (null == tableName) {
            return  new AjaxResult(ErrCode.SYS_PARAMETER_EMPTY.getErrCode(), ErrCode.SYS_PARAMETER_EMPTY.getErrMsg());
        }
        List<BmsMdBusinessCategory> businessCategorys = bmsMdBusinessCategoryService.selectBusinessCategoryAndChildrenNumList(businessCategory, tableName.name());
        List<TreeSelectAndChildrenNum> list = bmsMdBusinessCategoryService.buildBusinessTreeSelectAndChildrenNum(businessCategorys, false);
        return AjaxResult.success(list);
    }
    /**
     * 获取业务类别下拉树列表  1对多的 典型案例与 模板下载需要
     */
    @PreAuthorize("@ss.hasPermi('system:businessCategory:treeselectAndChildrenNum')")
    @GetMapping("/treeSelectOneToMore")
    public AjaxResult treeselectAndChildrenNumMore(BmsMdBusinessCategory businessCategory, BusinessCategoryUser tableName) {
        if (null == tableName) {
            return  new AjaxResult(ErrCode.SYS_PARAMETER_EMPTY.getErrCode(), ErrCode.SYS_PARAMETER_EMPTY.getErrMsg());
        }
        List<BmsMdBusinessCategory> businessCategorys = bmsMdBusinessCategoryService.selectBusinessCategorySelectOneToMore(businessCategory, tableName.name());
        List<TreeSelectAndChildrenNum> list = bmsMdBusinessCategoryService.buildBusinessTreeSelectAndChildrenNum(businessCategorys, false);
        return AjaxResult.success(list);
    }
    
    /**
     * 获取业务类别菜单
     */
    @GetMapping("/getFunOptions")
    public AjaxResult getFunOptions() {
    	List<TreeSelectAndChildrenNum> list= bmsMdBusinessCategoryService.selectFunOptions();
        return AjaxResult.success(list);
    }
    
    /**
     * 查询当前业务类别是否是招标代理
     */
    //@PreAuthorize("@ss.hasPermi('system:businessCategory:query')")
    @GetMapping(value = "/checkAgency/{businessTypeId}")
    public AjaxResult checkAgency(@PathVariable("businessTypeId") String businessTypeId) {
        return AjaxResult.success(bmsMdBusinessCategoryService.checkAgency(businessTypeId));
    }
}
